//
//  LeadPageViewController.m
//  news
//
//  Created by imacbookair on 16/3/11.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "LeadPageViewController.h"
#import "AppDelegate.h"

@interface LeadPageViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIScrollView *smallScrollView;
@property (nonatomic, assign)BOOL isOut;

@end

@implementation LeadPageViewController

- (void)viewWillAppear:(BOOL)animated
{


    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    _scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds) * 4, CGRectGetHeight(self.view.bounds));
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    
    for (NSInteger i = 0; i < 4; i++) {
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds) * i, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
        
//        UIImageView *imageView = [[UIImageView alloc]init];
//        
//        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(@(i * CGRectGetWidth(self.view.bounds)));
//            //            make.right.equalTo(@0);
//            make.width.equalTo(@(self.view.bounds.size.width));
//            make.top.equalTo(@0);
//            make.bottom.equalTo(@0);
//        }];
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", i + 1]];
        [_scrollView addSubview:imageView];
        
    }
    [self.view addSubview:_scrollView];
    
    
    UIImageView *smallImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds) / 4, CGRectGetHeight(self.view.frame) / 4, CGRectGetWidth(self.view.bounds) / 2, CGRectGetHeight(self.view.bounds) / 2)];
    
//    [smallImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@10);
//        make.top.equalTo(@10);
//        make.bottom.equalTo(@10);
//        make.right.equalTo(@10);
//        
//    }];
    smallImageView.userInteractionEnabled = YES;
    smallImageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:smallImageView];
    
    self.smallScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(smallImageView.bounds), CGRectGetHeight(smallImageView.bounds))];
    _smallScrollView.scrollEnabled = NO;
    _smallScrollView.delegate = self;
    _smallScrollView.contentSize = CGSizeMake(CGRectGetWidth(smallImageView.bounds) * 4, CGRectGetHeight(smallImageView.bounds));
    _smallScrollView.pagingEnabled = YES;
    _smallScrollView.bounces = NO;
    for (NSInteger i = 0; i < 4; i++) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(smallImageView.bounds) * i, 0, CGRectGetWidth(smallImageView.bounds), CGRectGetHeight(smallImageView.bounds))];
        image.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", i + 1]];
        [_smallScrollView addSubview:image];
        
    }
    [smallImageView addSubview:_smallScrollView];
    [self.view addSubview:_scrollView];
    [self.view addSubview:smallImageView];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x > 2 * self.view.bounds.size.width + 100) {
        _isOut = YES;
    }
    if (scrollView == _scrollView) {
        CGPoint point = _scrollView.contentOffset;
        point.y = point.y * 4;
        _smallScrollView.contentOffset = CGPointMake(_scrollView.contentOffset.x / 2, _scrollView.contentOffset.y);
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    if (_isOut) {
        [UIView animateWithDuration:1.5 animations:^{
            scrollView.alpha = 0;
            _smallScrollView.alpha = 0;
        } completion:^(BOOL finished) {
            [scrollView removeFromSuperview];
            app.window.rootViewController = app.tabBar;
        }];


    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
